/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.core.service.ruler.impl;

import com.playground.core.entity.Game;
import com.playground.core.entity.Player;
import com.playground.core.entity.boardgame.BoardGame;
import com.playground.core.entity.boardgame.Move;
import com.playground.core.entity.boardgame.Piece;
import com.playground.core.entity.boardgame.Square;
import com.playground.core.entity.boardgame.tictactoe.TicTacToeBoard;
import com.playground.core.service.ruler.TicTacToeRuler;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 *
 * @author Cleber
 */
@Service("ticTacToeRuler")
public class TicTacToeRulerImpl extends BoardGameRulerImpl implements TicTacToeRuler{
    private static int nextPieceID = 0;
    public boolean executeMove(BoardGame game, Move move) {
       if (isMoveAllowed(game,move)){
            Piece p = new Piece(nextPieceID++);
            p.setOwner(game.getPlayerInTurn());
            game.getBoard().addPieceAt(p,game.getBoard().getSquareAt(move.getDestination().getPosition()));
          //  game.numberOfMoves++;
            endTurn(game,move);
            return true;
        }
        return false;
    }

    public boolean isMoveAllowed(BoardGame game, Move move) {
       Square sq = game.getBoard().getSquareAt(move.getDestination().getPosition());
       return sq.isFree();
    }

    public void endTurn(BoardGame game, Move move) {
        game.setPlayerInTurn(game.getPlayers().get((game.getPlayers().indexOf(game.getPlayerInTurn()) + 1) % game.getPlayers().size()));   
    }

    public boolean isGameOver(Game game) {
        TicTacToeBoard board =  (TicTacToeBoard)((BoardGame)game).getBoard();
        
            //verify line 1
            
            Square s1 = board.getSquareAt(0);
            Square s2 = board.getSquareAt(1);
            Square s3 = board.getSquareAt(2);
            if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner());
                    return true;
                }
            }
            
            //verify line 2
            
            s1 = board.getSquareAt(3);
            s2 = board.getSquareAt(4);
            s3 = board.getSquareAt(5);
            if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner());
                    return true;
                }
            }
            
            //verify line 3
            
            s1 = board.getSquareAt(6);
            s2 = board.getSquareAt(7);
            s3 = board.getSquareAt(8);
            if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner()); 
                    return true;
                }
            }
            
             //verify collum 0
            
            s1 = board.getSquareAt(0);
            s2 = board.getSquareAt(3);
            s3 = board.getSquareAt(6);
             if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner()); 
                    return true;
                }
            }
            
             //verify collumn 1
            
            s1 = board.getSquareAt(1);
            s2 = board.getSquareAt(4);
            s3 = board.getSquareAt(7);
            if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner()); 
                    return true;
                }
            }
            
             //verify collumn 2
            
            s1 = board.getSquareAt(2);
            s2 = board.getSquareAt(5);
            s3 = board.getSquareAt(8);
            if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner()); 
                    return true;
                }
            }
            
             //verify left diagonal
            
            s1 = board.getSquareAt(0);
            s2 = board.getSquareAt(4);
            s3 = board.getSquareAt(8);
           if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner()); 
                    return true;
                }
            }
            
             //verify right diagonal
            
            s1 = board.getSquareAt(2);
            s2 = board.getSquareAt(4);
            s3 = board.getSquareAt(6);
           if (!s1.isFree() && !s2.isFree() && !s3.isFree()){
                if (s1.getPiece().getOwner().equals(s2.getPiece().getOwner()) && s1.getPiece().getOwner().equals(s3.getPiece().getOwner())){
                    game.setWinner(s1.getPiece().getOwner()); 
                    return true;
                }
            }
            
         if (!board.hasAvailableSquare())
            return true; 
         
        return false;
    }

   public List<Square> getAllowedSquares(BoardGame game) {
        List<Square> free = new ArrayList<Square>();
        for (int i = 0; i < 9; i ++){
            Square sq = game.getBoard().getSquareAt(i);
            if (sq.isFree())
                free.add(sq);
        }
        return free;
    }
   
    
}
